home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / wb / Tripppin.lha / Tripppin / source / board.c next >
C/C++ Source or Header  |  1991-01-10  |  8KB  |  311 lines

  1. /* This handles initializing and showing the game board in the game Trippin. */
  2.  
  3.  
  4. #include <graphics/gfx.h>
  5. #include <graphics/rastport.h>
  6. #include "trip.h"
  7.  
  8. /* a little substitute cause we don't need to know the contents: */
  9. struct Image { long image; };
  10.  
  11.  
  12. #define SQWID  (SQIZE << 1)
  13. #define BOWID  (SQIZE << 4)
  14. #define ARIZE  ((SQIZE >> 1) - 2)
  15. #define RADZ   (SQIZE / 3L)
  16.  
  17. #define ANY 0xFF
  18.  
  19.  
  20. import struct Image olabel, blabel;
  21.  
  22. import ubyte board[8][8];
  23.  
  24. import bool lace, won;
  25.  
  26. import short sqite, thite;
  27.  
  28. import struct RastPort *r;
  29.  
  30. import piece bb, oo, *turn;
  31.  
  32.  
  33. private ubyte combos[58] = {        /* all possible three-bits-true bytes */
  34.     0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x15, 0x16, 0x19, 0x1A, 0x1C, 0x23, 0x25,
  35.     0x26, 0x29, 0x2A, 0x2C, 0x31, 0x32, 0x34, 0x38, 0x43, 0x45, 0x46, 0x49,
  36.     0x4A, 0x4C, 0x51, 0x52, 0x54, 0x58, 0x61, 0x62, 0x64, 0x68, 0x70, 0x83,
  37.     0x85, 0x86, 0x89, 0x8A, 0x8C, 0x91, 0x92, 0x94, 0x98, 0xA1, 0xA2, 0xA4,
  38.     0xA8, 0xB0, 0xC1, 0xC2, 0xC4, 0xC8, 0xD0, 0xE0, ANY, ANY
  39. };
  40.  
  41.  
  42.  
  43. void Shuffle()
  44. {
  45.     ubyte deck[58];
  46.     register short x, y;
  47.     int rand(), i = 0;
  48.  
  49.     movmem(combos, deck, 58);
  50. /*  for (i = 0; i < 2; i++)        /* some extra randomness */
  51.     for (x = 0; x < 58; x++)
  52.         for (y = 0; y < 58; y++)
  53.         if (rand() & 1) {
  54.             register ubyte t = deck[x];
  55.             deck[x] = deck[y];
  56.             deck[y] = t;
  57.         }
  58.     for (x = 0; x < 8; x++)
  59.     for (y = 0; y < 8; y++)
  60.         if ((x == 1 || x == 6) && (y == 1 || y == 6))
  61.         board[x][y] = ANY;
  62.         else if (y == 7 && (x == 4 || x == 3))
  63.         board[x][y] = ANY;
  64.         else
  65.         board[x][y] = deck[i++];
  66. }
  67.  
  68.  
  69.  
  70. private void DrawArrows(xc, yc, bits) short xc, yc, bits;
  71. {
  72.     short sx = xc + (SQWID >> 1) + 1, sy = yc + (sqite >> 1);
  73.     short dx, dy, len, i;
  74.  
  75.     RectFill(r, sx - 3L, sy - (1L << lace),
  76.         sx + 2L, sy + (2L << lace) - 1);
  77.     for (i = 0; i < 8; i++)
  78.     if (bits & (1 << i)) {
  79.         short yo = lace && i >= 2 && i <= 6;
  80.         dx = sx;
  81.         dy = sy;
  82.         len = i & 1 ? ARIZE - (ARIZE >> 2) : ARIZE;
  83.         if (i >= 1 && i <= 3)
  84.         dx += len << 1;
  85.         else if (i >= 5)
  86.         dx -= (len << 1);
  87.         if (i >= 3 && i <= 5)
  88.         dy += len << lace;
  89.         else if (i <= 1 || i == 7)
  90.         dy -= len << lace;
  91.         if (lace && sx != dx) {        /* thicken lace non-verticals */
  92.         Move(r, (long) sx - (i >= 5), sy + 1L - yo);
  93.         Draw(r, (long) dx - (i >= 5), dy + 1L - yo);
  94.         }
  95.         dy += yo;
  96.         sy += yo;
  97.         Move(r, (long) sx, (long) sy);
  98.         Draw(r, (long) dx - (i == 6), (long) dy);
  99.         if (sy != dy) {            /* thicken non-horizontals */
  100.         Move(r, sx - 1L, (long) sy);
  101.         Draw(r, dx - 1L, (long) dy);
  102.         }
  103.         sy -= yo;
  104.         if (i & 1) {            /* diagonal line arrowheads */
  105.         long ye = dy + ((i == 3 || i == 5 ? -3L : 3L) << lace);
  106.         Move(r, (long) dx, (long) dy);
  107.         Draw(r, (long) dx, ye);
  108.         Move(r, dx - 1L, (long) dy);
  109.         Draw(r, dx - 1L, ye);
  110.         Move(r, (long) dx, (long) dy);
  111.         Draw(r, dx + (i >= 5 ? 6L - lace : -7L + lace), (long) dy);
  112.         if (lace) {
  113.             if (yo) dy--;
  114.             else dy++;
  115.             Move(r, (long) dx, (long) dy);
  116.             Draw(r, dx + (i >= 5 ? 6L - lace : -7L + lace), (long) dy);
  117.         }
  118.         } else {
  119.         long pull;
  120.         if (sx == dx) {                /* vertical lines */
  121.             pull = (i == 4 ? -2 : 2) << lace;
  122.             Move(r, (long) dx, (long) dy);
  123.             Draw(r, dx + 4L, dy + pull);
  124.             Move(r, dx - 1L, (long) dy);
  125.             Draw(r, dx - 5L, dy + pull);
  126.             if (lace) {
  127.             if (i) dy--;
  128.             else dy++;
  129.             Move(r, (long) dx, (long) dy);
  130.             Draw(r, dx + 4L, dy + pull);
  131.             Move(r, dx - 1L, (long) dy);
  132.             Draw(r, dx - 5L, dy + pull);
  133.             }
  134.         } else {                /* horizontal lines */
  135.             pull = i == 2 ? -4 : 4;
  136.             if (i == 6) dx--;
  137.             if (lace) {
  138.             Move(r, (long) dx, (long) dy);
  139.             Draw(r, dx + pull, dy + (2L << lace));
  140.             Move(r, (long) dx, (long) dy - yo);
  141.             Draw(r, dx + pull, dy - yo - (2L << lace));
  142.             }
  143.             dx += (short) pull >> 2;
  144.             Move(r, (long) dx, (long) dy);
  145.             Draw(r, dx + pull, dy + (2L << lace));
  146.             Move(r, (long) dx, (long) dy - yo);
  147.             Draw(r, dx + pull, dy - yo - (2L << lace));
  148.         }
  149.         }
  150.     }
  151. }
  152.  
  153.  
  154.  
  155. void DrawSquare(x, y) short x, y;
  156. {
  157.     long xc = 1 + x * SQWID, yc = thite + 2 + y * sqite,
  158.         hafite = sqite >> 1;
  159.  
  160.     SetAPen(r, WHITE);
  161.     RectFill(r, xc + 2, yc + 1 + lace, xc + SQWID - 1, yc + sqite - 1);
  162.     SetAPen(r, BLACK);
  163.     if ((x == 1 || x == 6) && (y == 1 || y == 6)) {
  164.     DrawEllipse(r, xc + (SQWID >> 1) + 1, yc + hafite,
  165.             RADZ << 1, RADZ << lace);
  166.     DrawEllipse(r, xc + (SQWID >> 1), yc + hafite,
  167.             RADZ << 1, RADZ << lace);
  168.     if (lace) {
  169.        DrawEllipse(r, xc + (SQWID >> 1) + 1, yc + hafite + 1,
  170.             RADZ << 1, RADZ << lace);
  171.        DrawEllipse(r, xc + (SQWID >> 1), yc + hafite + 1,
  172.             RADZ << 1, RADZ << lace);
  173.     }
  174.     } else if (y == 7 && (x == 3 || x == 4)) {
  175.     RectFill(r, xc + 8, yc + (4 << lace),
  176.             xc + SQWID - 7, yc + sqite - (4 << lace) + lace);
  177.     SetAPen(r, WHITE);
  178.     RectFill(r, xc + 14, yc + (7 << lace),
  179.             xc + SQWID - 13, yc + sqite - (7 << lace) + lace);
  180.     } else if (board[x][y] != ANY)
  181.     DrawArrows((short) xc, (short) yc, board[x][y]);
  182.     SetAPen(r, BLACK);
  183.     Move(r, xc + SQWID, yc);
  184.     Draw(r, xc, yc);
  185.     Draw(r, xc, yc + sqite);
  186.     Move(r, xc + 1, yc + sqite);
  187.     Draw(r, xc + 1, yc + 1);
  188.     if (lace) Draw(r, xc + SQWID, yc + 1);
  189. }
  190.  
  191.  
  192.  
  193. private void CenterText(say, y) str say; long y;
  194. {
  195.     long sl = strlen(say);
  196.     short tl = TextLength(r, say, sl);
  197.     Move(r, BOWID + (MARGINWID >> 1) - (tl >> 1) + 1L,
  198.         y + ((r->TxBaseline + 1) >> 1));
  199.     Text(r, say, sl);
  200. }
  201.  
  202.  
  203.  
  204. private long yoff(y) short y;
  205. {
  206.     return (long) (thite + 2 + ((y) << lace));
  207. }
  208.  
  209.  
  210.  
  211. void TellTurn()
  212. {
  213.     short xc, yc;
  214.     long a;
  215.     struct Image *label;
  216.     str say;
  217.     bool blueturn = turn == &bb;
  218.     static bool lastwon = true;
  219.  
  220.     SetAPen(r, WHITE);
  221.     RectFill(r, BOWID + 5L, yoff(2), BOWID + MARGINWID - 2L, yoff(21) - 1);
  222.     SetAPen(r, BLACK);
  223.     SetBPen(r, WHITE);
  224.     CenterText("To move:     ", yoff(11));
  225.     label = blueturn ? &blabel : &olabel;
  226.     DrawImage(r, label, BOWID - (IMWID >> 1) + 130L, yoff(11 - (IMHITE >> 1)));
  227.     SetAPen(r, blueturn ? BLUE : ORANGE);
  228.     RectFill(r, BOWID + 15L, yoff(32), BOWID + MARGINWID - 12L, yoff(56));
  229.     SetAPen(r, blueturn ? WHITE : BLACK);
  230.     SetBPen(r, blueturn ? BLUE : ORANGE);
  231.     if (won)
  232.     say = "Game Over.";
  233.     else if (!turn->allowed)
  234.     say = "CAN'T MOVE!";
  235.     else if (turn->machine)
  236.     say = "thinking...";
  237.     else
  238.     say = "MAKE A MOVE";
  239.     CenterText(say, yoff(44));
  240.     SetAPen(r, WHITE);
  241.     SetBPen(r, BLACK);
  242.     CenterText("Legal Moves:", yoff(74));
  243.     xc = BOWID + ((MARGINWID) >> 1) - SQIZE;
  244.     yc = yoff(82);
  245.     RectFill(r, xc + 2L, yc + lace + 1L, xc + SQWID - 1L, yc + sqite - 1L);
  246.     SetAPen(r, BLACK);
  247.     DrawArrows(xc, yc, won ? 0 : turn->allowed);
  248.     if (!turn->allowed)
  249.     won = true;
  250.     if (won == lastwon)
  251.     return;
  252.     lastwon = won;
  253.     if (won) {
  254.     SetAPen(r, ORANGE);
  255.     RectFill(r, BOWID + 7L, yoff(112), BOWID + MARGINWID - 4L, yoff(174));
  256.     SetAPen(r, WHITE);
  257.     RectFill(r, BOWID + 17L, yoff(117), BOWID + MARGINWID - 14L, yoff(169));
  258.     label = blueturn ? &olabel : &blabel;
  259.     a = yoff(120);
  260.     DrawImage(r, label, BOWID + 23L, a);
  261.     DrawImage(r, label, BOWID + MARGINWID - IMWID - 20L, a);
  262.     SetAPen(r, BLACK);
  263.     SetBPen(r, WHITE);
  264.     a = yoff(143) - ((thite + 1) >> 1);
  265.     CenterText("WE HAVE", a);
  266.     CenterText("A WINNER!", a + thite + 1);
  267.     a = yoff(167 - IMHITE);
  268.     DrawImage(r, label, BOWID + 23L, a);
  269.     DrawImage(r, label, BOWID + MARGINWID - IMWID - 20L, a);
  270.     } else {
  271.     SetAPen(r, BLACK);
  272.     RectFill(r, BOWID + 7L, yoff(112), BOWID + MARGINWID - 4L, yoff(174));
  273.     SetAPen(r, ORANGE);
  274.     SetBPen(r, BLACK);
  275.     CenterText("Try to reach", yoff(135) - thite);
  276.     CenterText("the blinking", yoff(136));
  277.     CenterText(" X  of your ", yoff(137) + thite);
  278.     CenterText("piece's color", yoff(138) + (thite << 1));
  279.     }
  280. }
  281.  
  282.  
  283.  
  284. void ShowBoard()
  285. {
  286.     short x, y, i, bwid;
  287.     long bot = 2 + thite + (sqite << 3);
  288.  
  289.     SetDrMd(r, JAM2);
  290.     SetAPen(r, BLACK);
  291.     RectFill(r, 3L + BOWID, thite + 2L,
  292.         2L + MARGINWID + BOWID, 2L + thite + (sqite << 3) + lace);
  293.     for (x = 0; x < 8; x++)
  294.     for (y = 0; y < 8; y++)
  295.         DrawSquare(x, y);
  296.     Move(r, BOWID + 2L, 2L + thite);
  297.     Draw(r, BOWID + 2L, bot);
  298.     Draw(r, 1L, bot);
  299.     Move(r, BOWID + 1L, 2L + thite);
  300.     Draw(r, BOWID + 1L, bot);
  301.     if (lace) {
  302.     Move(r, BOWID + 2L, bot + 1);
  303.     Draw(r, 1L, bot + 1);
  304.     }
  305.     LiftBob(&oo);            /* paint tokens in start position */
  306.     DropBob(&oo);
  307.     LiftBob(&bb);
  308.     DropBob(&bb);
  309.     TellTurn();
  310. }
  311.